package com.ruoyi.web.controller.form;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.form.domain.FormRegister;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.form.domain.FormOrder;
import com.ruoyi.form.service.IFormOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2024-11-21
 */
@Controller
@RequestMapping("/form/order")
public class FormOrderController extends BaseController
{
    private String prefix = "form/order";

    @Autowired
    private IFormOrderService formOrderService;
    @Autowired
    private ISysUserService userService;

//    @RequiresPermissions("form:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询订单列表
     */
//    @RequiresPermissions("form:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FormOrder formOrder)
    {
        startPage();
        List<FormOrder> list = formOrderService.selectFormOrderList(formOrder);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(e->{
                Long userId = e.getUserId();
                SysUser sysUser = userService.selectUserById(userId);
                if(sysUser != null){
                    e.setLoginName(sysUser.getLoginName());
                    e.setUserName(sysUser.getUserName());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("form:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FormOrder formOrder)
    {
        List<FormOrder> list = formOrderService.selectFormOrderList(formOrder);
        ExcelUtil<FormOrder> util = new ExcelUtil<FormOrder>(FormOrder.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        SysUser sysUser = new SysUser();
        sysUser.setUserType("01");
        model.addAttribute("userList",userService.selectUserListOwn(sysUser));
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("form:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FormOrder formOrder)
    {
        return toAjax(formOrderService.insertFormOrder(formOrder));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("form:order:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FormOrder formOrder = formOrderService.selectFormOrderById(id);
        mmap.put("formOrder", formOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("form:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormOrder formOrder)
    {
        return toAjax(formOrderService.updateFormOrder(formOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("form:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        List<String> list = Arrays.asList(ids.split(","));
        for (String s : list) {
            FormOrder formOrder = formOrderService.selectFormOrderById(Long.valueOf(s));
            SysUser sysUser = userService.selectUserById(formOrder.getUserId());
            if(sysUser != null){
                Integer sendCount = sysUser.getSendCount();
                Integer quantity = formOrder.getQuantity();
                if(sendCount != null && sendCount >=0 && quantity != null && quantity >=0){
                    if(quantity > sendCount){
                        return AjaxResult.warn("存在订单数量大于剩余数量，订单不能删除");
                    }
                }
            }
        }

        return toAjax(formOrderService.deleteFormOrderByIds(ids));
    }
}
